package com.yhp.servlet;

import com.yhp.bean.Users;
import com.yhp.service.impl.UsersServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(urlPatterns = "/mylogin")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收参数
        String username = req.getParameter("TxtUserName");
        String password = req.getParameter("TxtPassword");
        //2.调用service方法
        System.out.println("成功运行到这里了");
        UsersServiceImpl usersService = new UsersServiceImpl();
        Users users = usersService.login(username, password);
        System.out.println(users);
        if (users == null){
            resp.setContentType("text/html;charset=utf-8");
            PrintWriter writer = resp.getWriter();
            writer.println("<script>location.href='index.jsp';alert('用户名或密码不正确');</script>");
        }else {
            req.getSession().setAttribute("user",users);
            System.out.println("账号密码正确");
            resp.sendRedirect("main.jsp");
        }

    }
}
